<template>
  <div id="staModuleSeDiv">
    <Splitpanes style="height: 100%">
      <Pane size="50">
        <vxe-toolbar perfect>
          <template #buttons>
            <vxe-input style="width: 180px" v-model="searchForm.searchValue" type="search" clearable class="vxeInputSearch" placeholder="关键字查询" @clear="getStaModuleList" @keyup="keyupEnter" @search-click="getStaModuleList()"></vxe-input>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="刷新" :loading="tloading" @click="getStaModuleList"></vxe-button>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-square-plus" content="新建" @click="addStaModuleDialog()"></vxe-button>
            <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" @click="deleteStaModule"></vxe-button>
            <!-- <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="同步相关功能" @click="synModuleMenu"></vxe-button> -->
          </template>
        </vxe-toolbar>

        <div class="modulTable">
          <vxe-table ref="staModuleTableRef" class="mytable-scrollbar staModuleTableClazz" border="inner" align="center" height="auto" :data="staModuleList" :loading="tloading" @current-change="moduleRowClick" :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
            <vxe-column type="checkbox" width="30"></vxe-column>
            <vxe-column field="icon" title="图标" width="50">
              <template #default="{ row }">
                <el-icon size="20" class="icon">
                  <component :is="row.icon"></component>
                </el-icon>
              </template>
            </vxe-column>
            <vxe-column field="staModCode" title="标准编号" width="150">
            </vxe-column>
            <vxe-column field="modCode" title="模块编号" width="100">
            </vxe-column>
            <vxe-column field="modName" title="模块名称" width="100">
            </vxe-column>
            <vxe-column field="bigIcon" title="大图标" width="100">
            </vxe-column>
            <vxe-column field="phone" title="背景色" width="100">
              <template #default="{ row }">
                <span class="margin-left-xs padding-lr-xs" :style="{ background: `${row.bigIconBgcolour}` }">{{ row.bigIconBgcolour }}</span>
              </template>
            </vxe-column>
            <vxe-column field="baseUrl" title="路径">
            </vxe-column>
            <vxe-column field="isDefault" title="是否系统默认" width="100">
              <template #default="{ row }">
                <el-tag :type="getIsTagType(row.isDefault)">{{ getIsStatusDesc(row.isDefault) }}</el-tag>
              </template>
            </vxe-column>
            <vxe-column field="seq" title="顺序" width="50">
            </vxe-column>
          </vxe-table>
        </div>
      </Pane>
      <Pane size="50">
        <el-tabs type="border-card" v-model="activeName" @tab-click="tabHandleClick">
          <el-tab-pane label="基础信息" name="first">
            <vxe-toolbar perfect>
              <template #buttons>
                <vxe-button type="text" class="button-blue" @click="saveStaModule('staModuleFormRef')" icon="vxe-icon-square-plus" content="保存"></vxe-button>
              </template>
            </vxe-toolbar>
            <el-form ref="staModuleFormRef" :model="staModuleForm" :rules="staModuleFormRule" class="form-input-m" label-width="100px" inline-message>
              <el-row>
                <el-col :span="12">
                  <el-form-item prop="staModCode" label="标准编号">
                    <el-input type="text" v-model="staModuleForm.staModCode" placeholder="请输入标准编号" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="modCode" label="模块编号">
                    <el-input type="text" v-model="staModuleForm.modCode" placeholder="请输入模块编号" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="modName" label="模块名称">
                    <el-input type="text" v-model="staModuleForm.modName" placeholder="请输入模块名称	" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="icon" label="图标">
                    <el-input type="text" v-model="staModuleForm.icon" placeholder="请输入图标" clearable @click="showSelectIocn" ref="reficon"></el-input>
                    <SelectIcon ref="refselectIcon" @SelectIcon="selectIcon1"></SelectIcon>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="bigIcon" label="大图标">
                    <el-input type="text" v-model="staModuleForm.bigIcon" placeholder="请输入大图标" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="bigIconBgcolour" label="背景色">
                    <el-color-picker v-model="staModuleForm.bigIconBgcolour" show-alpha :predefine="predefineColors" />
                  </el-form-item>
                </el-col>

                <el-col :span="12">
                  <el-form-item prop="baseUrl" label="路径">
                    <el-input type="text" v-model="staModuleForm.baseUrl" placeholder="请输入路径" clearable></el-input>
                  </el-form-item>
                </el-col>

                <el-col :span="12">
                  <el-form-item prop="isDefault" label="是否系统默认">
                    <el-switch v-model="staModuleForm.isDefault" inline-prompt active-text="是" inactive-text="否" :active-value="1" :inactive-value="0"></el-switch>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="seq" label="顺序号">
                    <el-input type="text" v-model="staModuleForm.seq" placeholder="请输入顺序号" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item prop="description" label="功能描述">
                    <CkEditor ref="refCkEditor" :value="staModuleForm.description" folderId="system123" @editorInput="(val)=>staModuleForm.description=val" style="width:81%" />
                  </el-form-item>
                </el-col>

              </el-row>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="升级说明" name="upgradePane" :disabled="isEmpty(staModuleForm.id)">
            <StaModuleUpgrade ref="moduleUpgradeRef" />
          </el-tab-pane>
          <el-tab-pane label="相关功能" name="moduleMenuPane" :disabled="isEmpty(staModuleForm.id)">
            <StaModuleMenu ref="moduleMenuRef" />
          </el-tab-pane>
        </el-tabs>
      </Pane>
    </Splitpanes>
  </div>
</template>
<script setup lang="ts">
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
import { Splitpanes, Pane } from 'splitpanes';
import moment from 'moment';
let { proxy }: any = getCurrentInstance();
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage'; //提示
import { isEmpty } from 'lodash';
import SelectIcon from '@/components/SelectIcon.vue';
import StaModuleUpgrade from './StaModuleUpgrade.vue';
import StaModuleMenu from './StaModuleMenu.vue';
import CkEditor from '@/components/CkEditor.vue';

defineExpose({ initPage });
let searchForm = {
  searchValue: ''
};
let tloading = ref(false); //加载状态
let activeName = ref('first');
let refselectIcon = ref(null);
let staModuleList: any = reactive([]);
let sltStaModule: any = ref({});
let moduleUpgradeRef: any = ref(null);
let moduleMenuRef: any = ref(null);
let staModuleForm = reactive({
  id: null,
  staModCode: '',
  modCode: '',
  modName: '',
  icon: '',
  selectedIcon: '',
  bigIcon: '',
  baseUrl: '',
  isDefault: 0,
  bigIconBgcolour: '',
  bigIconColour: '',
  description: '',
  seq: 1,
  orgId: ''
});

let staModuleFormRule = {
  modName: [
    {
      required: true,
      message: '请输入供应商名称',
      trigger: 'blur'
    },
    {
      min: 2,
      max: 20,
      message: '长度为2到20个字符',
      trigger: 'blur'
    }
  ],
  modCode: [
    {
      required: true,
      message: '请输入供应商名称',
      trigger: 'blur'
    }
  ],
  staModCode: [
    {
      required: true,
      message: '请输入供应商名称',
      trigger: 'blur'
    }
  ]
};
function initPage() {
  getStaModuleList();
}

function getStaModuleList() {
  tloading.value = true;
  staModuleList.length = 0; //放入数据前清空数据
  const url = '/lis/sta/staModule/findStaModuleList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchForm.searchValue
      }
    })
    .then((res: any) => {
      staModuleList.push(...res.data);
      const kn = staModuleList.find((k) => k.id == sltStaModule.value.id);
      if (isEmpty(kn)) {
        moduleRowClick({ row: staModuleList[0] }); //默认选中第一条数据
      } else {
        moduleRowClick({ row: kn }); //默认选中第一条数据
      }
      tloading.value = false;
    })
    .catch((err: any) => {
      tloading.value = false;
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

function saveStaModule(formName: any) {
  let $el = proxy.$refs[formName];
  $el.validate((valid: any) => {
    if (!valid) {
      return false;
    }
    if (staModuleForm.id === null) {
      const url = '/lis/sta/staModule/addStaModule';
      const staModule = {
        staModCode: staModuleForm.staModCode,
        modCode: staModuleForm.modCode,
        modName: staModuleForm.modName,
        icon: staModuleForm.icon,
        selectedIcon: staModuleForm.selectedIcon,
        bigIcon: staModuleForm.bigIcon,
        baseUrl: staModuleForm.baseUrl,
        isDefault:staModuleForm.isDefault,
        bigIconBgcolour: staModuleForm.bigIconBgcolour,
        bigIconColour: staModuleForm.bigIconColour,
        description: staModuleForm.description,
        seq: staModuleForm.seq
      };
      proxy.$http
        .post(url, staModule)
        .then((res: any) => {
          VxeMessage('新增成功', VxeNotifyType.success);
          clearForm();
          sltStaModule.value.id = res.data.id;
          getStaModuleList();
        })
        .catch((err: any) => {
          VxeMessage('新增失败', VxeNotifyType.error);
        });
    } else {
      // 修改用户
      const url = '/lis/sta/staModule/editStaModule';

      const staModule = {
        id: staModuleForm.id,
        staModCode: staModuleForm.staModCode,
        modCode: staModuleForm.modCode,
        modName: staModuleForm.modName,
        icon: staModuleForm.icon,
        selectedIcon: staModuleForm.selectedIcon,
        bigIcon: staModuleForm.bigIcon,
        baseUrl: staModuleForm.baseUrl,
        isDefault:staModuleForm.isDefault,
        bigIconBgcolour: staModuleForm.bigIconBgcolour,
        bigIconColour: staModuleForm.bigIconColour,
        description: staModuleForm.description,
        seq: staModuleForm.seq
      };
      proxy.$http
        .post(url, staModule)
        .then((res: any) => {
          VxeMessage('修改成功', VxeNotifyType.success);
          sltStaModule.value.id = res.data.id;
          setTimeout(() => {
            clearForm();
            getStaModuleList();
          }, 50);
        })
        .catch((err: any) => {
          VxeMessage('修改失败', VxeNotifyType.error);
        });
    }
  });
}
function deleteStaModule() {
  const sltStaModules = (proxy.$refs.staModuleTableRef as any).getCheckboxRecords();
  if (!(sltStaModules.length > 0)) {
    VxeMessage('请勾选要删除的数据', VxeNotifyType.info);
  } else {
    proxy
      .$confirm('确定要删除当前勾选的数据？', '提示', {
        cancelButtonClass: 'cancel-button',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(async () => {
        //后台删除数据
        const delStaModuleList = sltStaModules.filter((p: any) => !isEmpty(p.id));
        if (delStaModuleList.length > 0) {
          const data = { staModuleList: delStaModuleList };
          const url = '/lis/sta/staModule/batchDeleteStaModuleList';
          proxy.$http
            .post(url, data)
            .then((res: any) => {
              getStaModuleList(); // 获取检验仪器表格数据
              VxeMessage(res.status.msg, VxeNotifyType.success);
            })
            .catch((err: any) => {
              VxeMessage(err, VxeNotifyType.error);
            });
        }
      })
      .catch((err: any) => {
        // VxeMessage(err, VxeNotifyType.error);
      });
  }
}
// 回车查询
function keyupEnter(e: any) {
  if (e.$event.code == 'Enter') {
    getStaModuleList();
  }
}
// 点击设计模式表格当前行触发右侧表单变化
function moduleRowClick({ row }: any) {
  if (isEmpty(row)) {
    clearForm();
    sltStaModule.value = {};
  } else {
    (proxy.$refs.staModuleTableRef as any).setCurrentRow(row);
    setStaModuleForm(row);
    if (activeName.value == 'upgradePane') {
      moduleUpgradeRef.value.initPage(staModuleForm);
    } else if (activeName.value == 'moduleMenuPane') {
      moduleMenuRef.value.initPage(staModuleForm);
    }
  }
}

function addStaModuleDialog() {
  clearForm();
  sltStaModule.value = {};
  (proxy.$refs.staModuleTableRef as any).clearCurrentRow();
  activeName.value = 'first';
}

// 清空中间规则弹框表单
function clearForm() {
  staModuleForm.id = null;
  staModuleForm.staModCode = '';
  staModuleForm.modCode = '';
  staModuleForm.modName = '';
  staModuleForm.icon = '';
  staModuleForm.selectedIcon = '';
  staModuleForm.bigIcon = '';
  staModuleForm.baseUrl = '';
  staModuleForm.isDefault=0;
  staModuleForm.bigIconBgcolour = '';
  staModuleForm.bigIconColour = '';
  staModuleForm.description = '';
  staModuleForm.seq = 1;
  staModuleForm.orgId = '';
  const temp: any = proxy.$refs['refCkEditor'];
  temp.setValue('');
}

// 编辑当前行规则
function setStaModuleForm(row: any) {
  staModuleForm.id = row.id;
  staModuleForm.staModCode = row.staModCode;
  staModuleForm.modCode = row.modCode;
  staModuleForm.modName = row.modName;
  staModuleForm.icon = row.icon;
  staModuleForm.selectedIcon = row.selectedIcon;
  staModuleForm.bigIcon = row.bigIcon;
  staModuleForm.baseUrl = row.baseUrl;
  staModuleForm.isDefault = row.isDefault;
  staModuleForm.bigIconBgcolour = row.bigIconBgcolour;
  staModuleForm.bigIconColour = row.bigIconColour;
  staModuleForm.seq = row.seq;
  staModuleForm.orgId = row.orgId;
  staModuleForm.description = row.description;
  const temp: any = proxy.$refs['refCkEditor'];
  if (isEmpty(staModuleForm.description)) {
    temp.setValue('');
  } else {
    temp.setValue(staModuleForm.description);
  }
  sltStaModule.value = row;
}

function tabHandleClick(tab: any) {
  if (tab.props.name == 'upgradePane') {
    moduleUpgradeRef.value.initPage(staModuleForm);
  } else if (tab.props.name == 'moduleMenuPane') {
    moduleMenuRef.value.initPage(staModuleForm);
  }
}

function synModuleMenu() {
  tloading.value = true;
  staModuleList.length = 0; //放入数据前清空数据
  const url = '/lis/sta/staModuleMenu/sysModuleMenu';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchForm.searchValue
      }
    })
    .then((res: any) => {
      staModuleList.push(...res.data);
      const kn = staModuleList.find((k) => k.id == sltStaModule.value.id);
      if (isEmpty(kn)) {
        moduleRowClick({ row: staModuleList[0] }); //默认选中第一条数据
      } else {
        moduleRowClick({ row: kn }); //默认选中第一条数据
      }
      tloading.value = false;
    })
    .catch((err: any) => {
      tloading.value = false;
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}
// 获取是否静态的tag标记(绿/红)
function getIsTagType(row: any) {
  if (row == '1') {
    return 'success';
  } else {
    return 'danger';
  }
}
// 获取是否静态文字(是/否)
function getIsStatusDesc(row: any) {
  if (row == '1') {
    return '是';
  } else {
    return '否';
  }
}
function selectIcon1(icon: any) {
  staModuleForm.icon = icon;
  setTimeout(() => {
    proxy.$refs.reficon.blur();
  }, 100);
}
function showSelectIocn() {
  refselectIcon.value.initPage(true);
  // console.log(refselectIcon.value.initPage);
}
const predefineColors = ref(['#ff4500', '#ff8c00', '#ffd700', '#90ee90', '#00ced1', '#1e90ff', '#c71585', 'rgba(255, 69, 0, 0.68)', 'rgb(255, 120, 0)', 'hsv(51, 100, 98)', 'hsva(120, 40, 94, 0.5)', 'hsl(181, 100%, 37%)', 'hsla(209, 100%, 56%, 0.73)', '#c7158577']);
</script>

<style lang="scss" scoped>
#staModuleSeDiv {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }

  //右侧质控项目表格
  .modulTable {
    height: calc(100% - 40px);
    background-color: $base-white;
  }
}
</style>